package com.jams.goodox.activity.serve.enterprise;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hangzhou.jin.customview.LoadView;
import com.hangzhou.jin.customview.TextMoreLayout;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.activity.common.SimpleHtmlActivity;
import com.jams.goodox.entity.resp.FinancialFirstPageResp;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.EnterpriseService;
import com.jams.goodox.utils.CollectionUtils;
import com.jams.goodox.utils.DensityUtil;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.library.commonAdapter.RecycleCommonAdapter;
import com.jams.library.commonAdapter.RecycleCommonViewHolder;
import com.jams.library.listener.OnRecycleItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;

import static com.jams.goodox.R.id.sdv_with_empty;


/**
 * 企业理财
 */
public class EnterpriseFinancialActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {


    @Bind(R.id.srf_business_department)
    SwipeRefreshLayout srf;
    @Bind(R.id.recycle_park)
    RecyclerView recycleParkBank;
    @Bind(R.id.recycle_other)
    RecyclerView recycleOtherBank;
    @Bind(R.id.empty_view)
    LoadView emptyView;
    @Bind(R.id.sdv_with_empty)
    SimpleDraweeView sdvBanner;

   

    /**
     * 园区营业部
     */
    RecycleCommonAdapter<FinancialFirstPageResp.FinancialFirstPageData.ManageListBean> parkBankAdapter;
    List<FinancialFirstPageResp.FinancialFirstPageData.ManageListBean> parkBankList = new ArrayList<>();
    /**
     * 其他营业部
     */
    RecycleCommonAdapter<FinancialFirstPageResp.FinancialFirstPageData.ManageListBean> otherBankAdapter;
    List<FinancialFirstPageResp.FinancialFirstPageData.ManageListBean> otherBankList = new ArrayList<>();

    public final static String MANAGE_PRODUCT_ID = "manageProductId";

    /**
     * 焦点图的链接
     */
    private String picSuperLink ;
    private TextMoreLayout tlDepartment;
    private TextMoreLayout tlOther;
    

    @Override
    protected int getLayoutId() {
        return R.layout.activity_enterprise_loan;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, "企业理财");
        srf.setOnRefreshListener(this);
        setData();
        onRefresh();
    }


    /**
     * 设置园区(其他)营业部数据,最多显示3个数据
     */
    private void setData() {
        recycleParkBank.setLayoutManager(new LinearLayoutManager(this));
        recycleParkBank.setHasFixedSize(true);
        recycleParkBank.setItemAnimator(new DefaultItemAnimator());
        recycleParkBank.setNestedScrollingEnabled(false);//设置recycleview跟随scrollview滚动
        parkBankAdapter = new RecycleCommonAdapter<FinancialFirstPageResp.FinancialFirstPageData.ManageListBean>(mContext,
                parkBankList,
                R.layout.item_bank_loan) {
            @Override
            public void convert(RecycleCommonViewHolder helper, final FinancialFirstPageResp.FinancialFirstPageData.ManageListBean item, int position) {
                helper.setText(R.id.tv_bank_name, StringUtils.newString(item.bankName))
                        .setText(R.id.tv_bank_address, item.departmentName)
                        .setText(R.id.tv_bank_tittle, item.lowLimit + "万起购, 利率最高" + item.highRate + "%")
                        .setSimpleDraweeView(R.id.sdv_icon, StringUtils.newString(item.icon));
                helper.setOnItemClickListener(new OnRecycleItemClickListener() {
                    @Override
                    public void itemClick() {
                        Intent intent = new Intent(mContext, EnterpriseFinancialDetailActivity.class);
                        intent.putExtra(MANAGE_PRODUCT_ID, StringUtils.newString(item.manageProductId));
                        startActivity(intent);
                    }
                });
            }
        };
        addHeadView(true);
        recycleParkBank.setAdapter(parkBankAdapter);
        //其他营业部数据
        recycleOtherBank.setLayoutManager(new LinearLayoutManager(this));
        recycleOtherBank.setHasFixedSize(true);
        recycleOtherBank.setItemAnimator(new DefaultItemAnimator());
        recycleOtherBank.setNestedScrollingEnabled(false);
        otherBankAdapter = new RecycleCommonAdapter<FinancialFirstPageResp.FinancialFirstPageData.ManageListBean>(mContext, otherBankList, R.layout.item_bank_loan) {
            @Override
            public void convert(RecycleCommonViewHolder helper, final FinancialFirstPageResp.FinancialFirstPageData.ManageListBean item, int position) {
                helper.setText(R.id.tv_bank_name, StringUtils.newString(item.bankName))
                        .setText(R.id.tv_bank_address, item.departmentName)
                        .setText(R.id.tv_bank_tittle, item.lowLimit + "万起购, 利率最高" + item.highRate + "%")
                        .setSimpleDraweeView(R.id.sdv_icon, StringUtils.newString(item.icon));
                helper.setOnItemClickListener(new OnRecycleItemClickListener() {
                    @Override
                    public void itemClick() {
                        Intent intent = new Intent(mContext, EnterpriseFinancialDetailActivity.class);
                        intent.putExtra(MANAGE_PRODUCT_ID, StringUtils.newString(item.manageProductId));
                        startActivity(intent);
                    }
                });
            }
        };
        addHeadView(false);
        recycleOtherBank.setAdapter(otherBankAdapter);
        ViewGroup.LayoutParams params = sdvBanner.getLayoutParams();
        params.height = (int) (0.375 * DensityUtil.getDisplayWidth(mContext));
        sdvBanner.setLayoutParams(params);
        emptyView.loadCompleteNoData(R.mipmap.holder_no_department, R.string.no_department);
        GenericDraweeHierarchy hierarchy = sdvBanner.getHierarchy();
        hierarchy.setPlaceholderImage(R.mipmap.banner_enterprise_finicial);//默认banner图
    }


    /**
     * 请求理财数据
     */
    private void requestFinancialData() {
        enqueue(createCallService(EnterpriseService.class).getFinancialFirstPage(), new RetrofitCallbackListener<FinancialFirstPageResp>() {
            @Override
            public void onSuccess(Call<FinancialFirstPageResp> call, FinancialFirstPageResp result) {
                misLoading();
                try {
                    srf.setRefreshing(false);
                    List<FinancialFirstPageResp.FinancialFirstPageData.ManageListBean> parkList = result.data.communityManageList;
                    List<FinancialFirstPageResp.FinancialFirstPageData.ManageListBean> otherList = result.data.otherManageList;
                    parkBankList.clear();
                    otherBankList.clear();
                    if (CollectionUtils.hasData(parkList)) {
                        parkBankList.addAll(parkList.size() < 3 ? parkList : parkList.subList(0, 3));//最多显示3个条目
                    }
                    if (CollectionUtils.hasData(otherList)) {
                        otherBankList.addAll(otherList.size() < 3 ? otherList : otherList.subList(0, 3));//最多显示3个条目
                    }
                    picSuperLink = StringUtils.newString(result.data.picSuperLink);
                    sdvBanner.setImageURI(StringUtils.newString(result.data.focusPic));
                    parkBankAdapter.notifyDataSetChanged();
                    otherBankAdapter.notifyDataSetChanged();
                    tlDepartment.setVisibility(CollectionUtils.hasData(parkBankList) ? View.VISIBLE : View.GONE);
                    tlOther.setVisibility(CollectionUtils.hasData(otherBankList) ? View.VISIBLE : View.GONE);
                } catch (Exception e) {
                    e.printStackTrace();
                    srf.setRefreshing(false);
                }
            }

            @Override
            public void onError(String msg) {
                if (srf != null) {
                    srf.setRefreshing(false);
                }
            }
        });

    }

    @Override
    public void onRefresh() {
        try {
            srf.setRefreshing(true);
            requestFinancialData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加 recycleview 的 headview
     *
     * @param isFirst true:园区营业部 false:其他营业部
     */
    private void addHeadView(boolean isFirst) {
        if (isFirst) {
            final View v1 = View.inflate(mContext, R.layout.item_top_view_lend, null);
            tlDepartment = (TextMoreLayout) v1.findViewById(R.id.tl_park_business_department);
            tlDepartment.setLeftText(getResources().getString(R.string.park_business_department));
            tlDepartment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, FinancialDepartmentListActivity.class);
                    intent.putExtra("TITTLE", getResources().getString(R.string.park_business_department));
                    startActivity(intent);
                }
            });
            parkBankAdapter.setHeaderView(v1);
        } else {
            final View v2 = View.inflate(mContext, R.layout.item_top_view_lend, null);
            tlOther = (TextMoreLayout) v2.findViewById(R.id.tl_park_business_department);
            tlOther.setLeftText(getResources().getString(R.string.other_business_department));
            tlOther.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, FinancialDepartmentListActivity.class);
                    intent.putExtra("TITTLE", getResources().getString(R.string.other_business_department));
                    startActivity(intent);
                }
            });
            otherBankAdapter.setHeaderView(v2);
        }


    }
    

    @OnClick(sdv_with_empty)
    public void onViewClicked() {
        if (TextUtils.isEmpty(picSuperLink)) { //链接为空,返回
            return;
        }
        Intent intent = new Intent(mContext, SimpleHtmlActivity.class);
        intent.putExtra(SimpleHtmlActivity.URL, picSuperLink);
        intent.putExtra(SimpleHtmlActivity.TITLE,"详情");
        qStartActivity(intent);
    }
}
